<template>
    <div class="main_box" style="padding:10px">
      <div class="add_wrap">
        <div class="tit">
          <h2>{{dataForm.id?'编辑翼码商户号':'新增翼码商户号'}}</h2>
          <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
        </div>
        <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules">
          <el-row>
            <el-form-item prop="name" label="商户号名称">
              <el-input type="text" v-model="dataForm.name" placeholder="请输入商户号名称"></el-input>
            </el-form-item>
            <el-form-item prop="isspId" label="翼码商户号" >
              <el-input type="text" v-model="dataForm.isspId" clearable placeholder="请输入翼码商户号"></el-input>
            </el-form-item>

            <el-form-item prop="systemId" label="平台号" >
              <el-input type="text" v-model="dataForm.systemId" clearable placeholder="请输入平台号"></el-input>
            </el-form-item>
            <el-form-item prop="systemKey" label="平台密钥" >
              <el-input type="text" v-model="dataForm.systemKey" clearable placeholder="请输入平台密钥"></el-input>
            </el-form-item>
            <el-form-item prop="posId" label="终端号" >
              <el-input type="text" v-model="dataForm.posId" clearable placeholder="请输入终端号"></el-input>
            </el-form-item>
            <el-form-item prop="remark" label="备注">
              <el-input type="textarea" v-model="dataForm.remark"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item>
              <el-button plain type="primary" @click="sureClick">保 存</el-button>
              <el-button plain @click="returnList">取 消</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </div>
    </div>
  </template>

  <script>
    import * as message from "@/constants/message";
    import YmMerchantApi from '@/api/account/ymMerchant';

    export default {
      data() {
        return {
          dataForm: {
            id:"",
            name: "",
            isspId: "",
            systemId: "",
            systemKey:"",
            posId:"",
            remark: "",
          },
          tenantList:[],
          roleRules: {
            name: [
              {required: true, message: "请输入商户号", trigger: "focus"}
            ],
            isspId:[
              {required:true,message:"请输入翼码商户号",trigger:"focus"}
            ],
            systemId: [
              {required: true, message: "请输入平台号", trigger: "focus"}
            ],
            systemKey: [
              {required: true, message: "请输入平台密钥", trigger: "focus"}
            ],
            posId: [
              {required: true, message: "请输入终端号", trigger: "focus"}
            ],
            tenantId: [
            {required: true, message: "请选择租户", trigger: "change"}
          ],
          }
        }
      },
      components: {
      },
      mounted() {
        this.init()
      },
      methods: {
        init() {
          if (this.$route.query.id) {
            this.dataForm.id = this.$route.query.id
            this.getData()
          }
          // this.getTenantList()
        },
        keydown(e) {
          if (e.keyCode == 32) {
            e.returnValue = false;
          }
        },
        getData() {
          YmMerchantApi.getYmMerchant({
            id: this.dataForm.id
          }).then(res => {
            this.$Utils.bindingBean(this.dataForm, res.data)
          })
        },
        sureClick() {
          this.$refs.dataForm.validate(valid => {
            if (valid) {
              this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
                .then(() => {
                  if (this.dataForm.id) {
                    this.editData();
                  } else {
                    this.addData();
                  }
                });
            }
          })
        },
        addData() {
          let param = {
            departId: this.dataForm.departId,
            name: this.dataForm.name,
            isspId: this.dataForm.isspId,
            systemId: this.dataForm.systemId,
            systemKey:this.dataForm.systemKey,
            posId:this.dataForm.posId,
            tenantId:this.dataForm.tenantId,
            remark: this.dataForm.remark
          };

          YmMerchantApi.addYmMerchant(param).then(res => {
            this.$Utils.notifySuccess()
            this.returnList();
          })
        },
        editData() {
          let param = {
            id:this.dataForm.id,
            departId:this.dataForm.departId,
            name: this.dataForm.name,
            isspId: this.dataForm.isspId,
            systemId: this.dataForm.systemId,
            systemKey:this.dataForm.systemKey,
            posId:this.dataForm.posId,
            tenantId:this.dataForm.tenantId,
            remark: this.dataForm.remark
          };
          YmMerchantApi.editYmMerchant(param)
            .then(data => {
              this.$Utils.notifySuccess()
              this.returnList();
            })
        },
        returnList() {
          this.$store.dispatch('delView', this.$route);
          this.$router.push({path: '/account/YmMerchantManagement'});
        }
      },

    }
  </script>
<style lang="scss" scoped>
@import "@/assets/css/views/add.scss";
</style>


